/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fh.view;
import fh.view.running.CurrentTourneyOverview;
import fh.controller.EventController;
import fh.model.Event;
import fh.view.masterclass.*;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Dikkehein
 */
public class MasterclassOverview extends javax.swing.JFrame {

    Event masterclass;
    EventController mc;

    /**
     * Creates new form MasterclassOverview
     */
    public MasterclassOverview() {
        initComponents();
        masterclass = new Event();
        mc = new EventController();
        FillList();
    }

    private void FillList() {
        DefaultComboBoxModel tourney = mc.getMasterclassesList();
        MasterclassOverviewList.clearSelection();
        MasterclassOverviewList.setModel(tourney);
    }

    private Event getMasterclass() {
        Event aMasterclass = (Event) MasterclassOverviewList.getSelectedValue();

        return aMasterclass;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        checkbox1 = new java.awt.Checkbox();
        jLabel1 = new javax.swing.JLabel();
        CurrentTourenyButton = new javax.swing.JButton();
        PlayerOverviewButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        MasterclassOverviewList = new javax.swing.JList();
        DetailMasterclassButton = new javax.swing.JButton();
        EditMasterclassButon = new javax.swing.JButton();
        DeleteMasterclassButton = new javax.swing.JButton();
        AddMasterclassButton = new javax.swing.JButton();
        LocationEventMainButton = new javax.swing.JButton();
        PlayerRegistrationMasterclassButton = new javax.swing.JButton();

        checkbox1.setLabel("checkbox1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Masterclass overzicht");
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 30)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Full House");
        jLabel1.setToolTipText("");

        CurrentTourenyButton.setText("Toernooi runnen");
        CurrentTourenyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CurrentTourenyButtonActionPerformed(evt);
            }
        });

        PlayerOverviewButton.setText("Speler beheer");
        PlayerOverviewButton.setMaximumSize(new java.awt.Dimension(111, 23));
        PlayerOverviewButton.setMinimumSize(new java.awt.Dimension(111, 23));
        PlayerOverviewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlayerOverviewButtonActionPerformed(evt);
            }
        });

        MasterclassOverviewList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(MasterclassOverviewList);

        DetailMasterclassButton.setText("Details");
        DetailMasterclassButton.setMaximumSize(new java.awt.Dimension(111, 23));
        DetailMasterclassButton.setMinimumSize(new java.awt.Dimension(111, 23));
        DetailMasterclassButton.setPreferredSize(new java.awt.Dimension(111, 23));
        DetailMasterclassButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DetailMasterclassButtonActionPerformed(evt);
            }
        });

        EditMasterclassButon.setText("Wijzigen");
        EditMasterclassButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditMasterclassButonActionPerformed(evt);
            }
        });

        DeleteMasterclassButton.setText("Verwijderen");
        DeleteMasterclassButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteMasterclassButtonActionPerformed(evt);
            }
        });

        AddMasterclassButton.setText("Toevoegen");
        AddMasterclassButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddMasterclassButtonActionPerformed(evt);
            }
        });

        LocationEventMainButton.setText("Locatie & Event");
        LocationEventMainButton.setMaximumSize(new java.awt.Dimension(111, 23));
        LocationEventMainButton.setMinimumSize(new java.awt.Dimension(111, 23));
        LocationEventMainButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LocationEventMainButtonActionPerformed(evt);
            }
        });

        PlayerRegistrationMasterclassButton.setText("Speler inschrijven");
        PlayerRegistrationMasterclassButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlayerRegistrationMasterclassButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LocationEventMainButton, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(PlayerOverviewButton, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(CurrentTourenyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(DetailMasterclassButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(EditMasterclassButon, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(DeleteMasterclassButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(70, 70, 70)
                        .addComponent(PlayerRegistrationMasterclassButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                        .addComponent(AddMasterclassButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PlayerOverviewButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CurrentTourenyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LocationEventMainButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AddMasterclassButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DeleteMasterclassButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(EditMasterclassButon, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DetailMasterclassButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PlayerRegistrationMasterclassButton))
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CurrentTourenyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CurrentTourenyButtonActionPerformed
        new CurrentTourneyOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_CurrentTourenyButtonActionPerformed

    private void PlayerOverviewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlayerOverviewButtonActionPerformed
        new PlayerOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_PlayerOverviewButtonActionPerformed

    private void EditMasterclassButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditMasterclassButonActionPerformed
        if (MasterclassOverviewList.getSelectedValue() == null) {

            JOptionPane.showMessageDialog(null, "Geen masterclass geselecteerd");
        } else {
            masterclass = getMasterclass();
            new EditMasterclass(masterclass).setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_EditMasterclassButonActionPerformed

    private void DeleteMasterclassButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteMasterclassButtonActionPerformed
        if (MasterclassOverviewList.getSelectedValue() == null) {

            JOptionPane.showMessageDialog(null, "Geen masterclass geselecteerd");
        } else {
            int antwoord = JOptionPane.showConfirmDialog(null, "Wilt u deze masterclass verwijderen?\n", "Verwijder masterclass", JOptionPane.YES_NO_OPTION);
            if (antwoord == JOptionPane.YES_OPTION) {


                masterclass = getMasterclass();
                int result = mc.deleteEventById(masterclass.getId());
                if (result >= 0) {
                    FillList();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Er is iets mis gegaan met verwijderen.");
                }
            }
        }
    }//GEN-LAST:event_DeleteMasterclassButtonActionPerformed

    private void AddMasterclassButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddMasterclassButtonActionPerformed
        new AddMasterclass().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_AddMasterclassButtonActionPerformed

    private void LocationEventMainButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LocationEventMainButtonActionPerformed
        new LocationEventOverview().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_LocationEventMainButtonActionPerformed

    private void DetailMasterclassButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DetailMasterclassButtonActionPerformed
        if (MasterclassOverviewList.getSelectedValue() == null) {

            JOptionPane.showMessageDialog(null, "Geen masterclass geselecteerd");
        } else {
            masterclass = getMasterclass();
            new DetailMasterclass(masterclass).setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_DetailMasterclassButtonActionPerformed

    private void PlayerRegistrationMasterclassButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlayerRegistrationMasterclassButtonActionPerformed

        if (MasterclassOverviewList.getSelectedValue() == null) {

            JOptionPane.showMessageDialog(null, "Geen masterclass geselecteerd");
        } else {


            Date date = new Date();
            masterclass = getMasterclass();
            int e_id = masterclass.getId();
            int count = mc.getRegistratedPlayers(e_id);
            if (date.before(masterclass.getDate())) {

                if (count < masterclass.getMax_players()) {
                    new PlayerRegistrationMasterclass(masterclass).setVisible(true);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Inschrijvingen zijn vol.");

                }
            } else {
                JOptionPane.showMessageDialog(rootPane, "Inschrijvingen zijn gesloten.");
            }
        }


    }//GEN-LAST:event_PlayerRegistrationMasterclassButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MasterclassOverview.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MasterclassOverview.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MasterclassOverview.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MasterclassOverview.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MasterclassOverview().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddMasterclassButton;
    private javax.swing.JButton CurrentTourenyButton;
    private javax.swing.JButton DeleteMasterclassButton;
    private javax.swing.JButton DetailMasterclassButton;
    private javax.swing.JButton EditMasterclassButon;
    private javax.swing.JButton LocationEventMainButton;
    private javax.swing.JList MasterclassOverviewList;
    private javax.swing.JButton PlayerOverviewButton;
    private javax.swing.JButton PlayerRegistrationMasterclassButton;
    private java.awt.Checkbox checkbox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
